package com.example.shop.response;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;


@XmlRootElement(
        name = "response"
)
@XmlAccessorType(XmlAccessType.FIELD)
public class BaseResponse<T> {

    @XmlJavaTypeAdapter(ToStringFormatter.class)
    @XmlElement(
            name = "code"
    )
    private Integer code = 0;

    @XmlElement(
            name = "message"
    )
    private String message = "success";

    @XmlElement(
            name = "requestId"
    )
    private String requestId;

    @XmlElement(
            name = "data"
    )
    private T data;

    public BaseResponse() {
    }

    public int getCode() {
        return this.code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return this.message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getRequestId() {
        return this.requestId;
    }

    public void setRequestId(String requestId) {
        this.requestId = requestId;
    }

    public static <T> BaseResponse.Builder<T> builder() {
        return new BaseResponse.Builder();
    }

    public static class Builder<T> {
        private final BaseResponse<T> response = new BaseResponse();

        Builder() {
        }

        public BaseResponse.Builder<T> code(int code) {
            this.response.setCode(code);
            return this;
        }

        public BaseResponse.Builder<T> message(String message) {
            this.response.setMessage(message);
            return this;
        }

        public BaseResponse.Builder<T> data(T data) {
            this.response.setData(data);
            return this;
        }

        public BaseResponse.Builder<T> requestId(String requestId) {
            this.response.setRequestId(requestId);
            return this;
        }

        public BaseResponse<T> get() {
            return this.response;
        }

    }
}